﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.Serialization;

namespace Toybox.NicoSharp.Live.LiveGuide
{
	using Extensions;
	using Logger;
	using Utilities;

	/// <summary>
	/// ZappingList ルートタイプ
	/// </summary>
	public enum ZRouteType
	{
		/// <summary>
		/// 注目番組
		/// </summary>
		index,

		/// <summary>
		/// お気に入り
		/// </summary>
		subscribe,

		/// <summary>
		/// 各種タグ
		/// </summary>
		recent,

		/// <summary>
		/// 検索結果
		/// </summary>
		search,
	}

	/// <summary>
	/// ZappingList クラス
	/// </summary>
	public partial class ZappingList
	{

		#region [Static]

		/// <summary>
		/// ZappingList 取得 (注目番組 or お気に入り)
		/// </summary>
		/// <param name="zroute">index or subscribe</param>
		/// <param name="zpage"></param>
		/// <param name="cookies"></param>
		/// <returns></returns>
		public static ZappingList Get(ZRouteType zroute, int zpage = 1, CookieCollection cookies = null)
		{
			Dictionary<string, string> dict = new Dictionary<string, string>();
			dict.Add("zroute", Enum.GetName(zroute.GetType(), zroute));
			dict.Add("zpage", zpage.ToString());

			return GetZappingList(dict, cookies);
		}

		/// <summary>
		/// ZappingList 取得 (各種タグ)
		/// </summary>
		/// <param name="tab"></param>
		/// <param name="sort"></param>
		/// <param name="isOrderDesc"></param>
		/// <param name="tags"></param>
		/// <param name="zpage"></param>
		/// <param name="cookies"></param>
		/// <returns></returns>
		public static ZappingList GetRecent(LiveTabType tab, LiveSortType sort, bool isOrderDesc, string tags, int zpage = 1, CookieCollection cookies = null)
		{
			Dictionary<string, string> dict = new Dictionary<string, string>();
			dict.Add("zroute", Enum.GetName(typeof(ZRouteType), ZRouteType.recent));
			dict.Add("tab", tab.ToSign());
			dict.Add("sort", sort.ToSign());
			dict.Add("order", (isOrderDesc ? "desc" : "asc"));
			dict.Add("tags", string.IsNullOrEmpty(tags) ? string.Empty : Uri.EscapeUriString(tags));
			dict.Add("zpage", zpage.ToString());

			return GetZappingList(dict, cookies);
		}

		/// <summary>
		/// ZappingList 取得 (検索結果)
		/// </summary>
		/// <param name="keyword"></param>
		/// <param name="filter"></param>
		/// <param name="zpage"></param>
		/// <param name="cookies"></param>
		/// <returns></returns>
		public static ZappingList GetSearch(string keyword, string filter, int zpage = 1, CookieCollection cookies = null)
		{
			Dictionary<string, string> dict = new Dictionary<string, string>();
			dict.Add("zroute", Enum.GetName(typeof(ZRouteType), ZRouteType.search));
			dict.Add("keyword", string.IsNullOrEmpty(keyword) ? string.Empty : Uri.EscapeUriString(keyword));
			//dict.Add("sort", Enum.GetName(sort.GetType(), sort));
			dict.Add("sort", string.Empty);
			//dict.Add("orig_filter", string.IsNullOrEmpty(orig_filter) ? string.Empty : Uri.EscapeUriString(orig_filter));
			dict.Add("orig_filter", string.Empty);
			dict.Add("filter", string.IsNullOrEmpty(filter) ? string.Empty : Uri.EscapeUriString(filter));

			return GetZappingList(dict, cookies);
		}

		/// <summary>
		/// ZappingList 取得
		/// </summary>
		/// <param name="dict"></param>
		/// <param name="cookies"></param>
		/// <returns></returns>
		protected static ZappingList GetZappingList(IDictionary<string, string> dict, CookieCollection cookies)
		{
			string url = NicoUrls.LiveApi + "getzappinglist?" + Serializer.AndEqualPairSerializer(dict); ;
			try
			{
				string json = NicoWebClient.Get(url, cookies.Coerce());
				return Deserializer.JsonDeserializer<ZappingList>(json);
			}
			catch (Exception ex)
			{
				TraceLogger.WriteLine(ex.ToString());
				return null;
			}
		}

		#endregion [Static]


		#region Public Members

		/// <summary>
		/// 放送中リスト
		/// </summary>
		[DataMember]
		public OnAirStream[] onair_stream_list;

		/// <summary>
		/// 予約リスト
		/// </summary>
		[DataMember]
		public ReservedStream[] reserved_stream_list;

		#endregion Public Members

	}
}
